#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
直接测试优化后的计算器函数
"""

import sys
import os

# 添加当前目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

# 导入main模块中的函数
from main import (
    add, subtract, multiply, divide,
    square, square_root, absolute, power,
    sin, cos, tan, log, ln, factorial,
    degrees_to_radians, radians_to_degrees,
    exp, ceil, floor, round_number,
    say_hello, say_goodbye
)

def test_functions():
    """直接测试所有函数"""
    print("=== 增强版MCP计算器直接函数测试 ===")
    print()
    
    # 测试基础运算
    print("📊 基础运算测试:")
    try:
        print(f"  ✅ 加法: 10 + 5 = {add(10, 5)}")
        print(f"  ✅ 减法: 10 - 3 = {subtract(10, 3)}")
        print(f"  ✅ 乘法: 6 × 7 = {multiply(6, 7)}")
        print(f"  ✅ 除法: 15 ÷ 3 = {divide(15, 3)}")
    except Exception as e:
        print(f"  ❌ 基础运算错误: {e}")
    
    print()
    
    # 测试科学计算
    print("🔬 科学计算测试:")
    try:
        print(f"  ✅ 平方: 8² = {square(8)}")
        print(f"  ✅ 平方根: √64 = {square_root(64)}")
        print(f"  ✅ 绝对值: |-15| = {absolute(-15)}")
        print(f"  ✅ 幂运算: 2^8 = {power(2, 8)}")
        print(f"  ✅ 指数: e^2 = {exp(2)}")
        print(f"  ✅ 阶乘: 5! = {factorial(5)}")
    except Exception as e:
        print(f"  ❌ 科学计算错误: {e}")
    
    print()
    
    # 测试三角函数
    print("📐 三角函数测试:")
    try:
        rad_90 = degrees_to_radians(90)
        print(f"  ✅ 角度转弧度: 90° = {rad_90:.4f} 弧度")
        print(f"  ✅ 正弦: sin(π/2) = {sin(1.5708):.4f}")
        print(f"  ✅ 余弦: cos(0) = {cos(0):.4f}")
        print(f"  ✅ 正切: tan(π/4) = {tan(0.7854):.4f}")
        print(f"  ✅ 弧度转角度: {rad_90:.4f} 弧度 = {radians_to_degrees(rad_90):.1f}°")
    except Exception as e:
        print(f"  ❌ 三角函数错误: {e}")
    
    print()
    
    # 测试对数函数
    print("📈 对数函数测试:")
    try:
        print(f"  ✅ 自然对数: ln(e) = {ln(2.718):.4f}")
        print(f"  ✅ 常用对数: log₁₀(100) = {log(100, 10)}")
        print(f"  ✅ 二进制对数: log₂(8) = {log(8, 2)}")
    except Exception as e:
        print(f"  ❌ 对数函数错误: {e}")
    
    print()
    
    # 测试取整函数
    print("🔢 取整函数测试:")
    try:
        print(f"  ✅ 向上取整: ceil(3.2) = {ceil(3.2)}")
        print(f"  ✅ 向下取整: floor(3.8) = {floor(3.8)}")
        print(f"  ✅ 四舍五入: round(3.14159, 2) = {round_number(3.14159, 2)}")
        print(f"  ✅ 四舍五入: round(3.14159, 4) = {round_number(3.14159, 4)}")
    except Exception as e:
        print(f"  ❌ 取整函数错误: {e}")
    
    print()
    
    # 测试问候功能
    print("👋 问候功能测试:")
    try:
        hello_msg = say_hello("李宛津")
        goodbye_msg = say_goodbye("李宛津")
        print(f"  ✅ 问候: {hello_msg}")
        print(f"  ✅ 告别: {goodbye_msg}")
    except Exception as e:
        print(f"  ❌ 问候功能错误: {e}")
    
    print()
    
    # 测试错误处理
    print("⚠️  错误处理测试:")
    error_tests = [
        ("除零", lambda: divide(10, 0)),
        ("负数开方", lambda: square_root(-4)),
        ("负数对数", lambda: log(-1)),
        ("负数阶乘", lambda: factorial(-1)),
        ("空用户名", lambda: say_hello(""))
    ]
    
    for test_name, test_func in error_tests:
        try:
            result = test_func()
            print(f"  ⚠️  {test_name} 应该失败但成功了: {result}")
        except Exception as e:
            print(f"  ✅ {test_name} 正确处理错误: {str(e)[:50]}...")
    
    print()
    print("🎉 直接函数测试完成！")
    print("✨ 优化功能包括:")
    print("   - 完善的错误处理和输入验证")
    print("   - 详细的日志记录")
    print("   - 角度弧度转换")
    print("   - 指数函数 (e^x)")
    print("   - 取整函数 (ceil, floor, round)")
    print("   - 增强的问候功能")
    print("   - 防止数值溢出保护")

if __name__ == "__main__":
    test_functions()